import router from '@/router'
import { store } from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
//权限验证处理:全集守卫路由实现
const whiteList = ['/login'] //白名单
router.beforeEach(async (to, from, next) => {
  NProgress.start()
  //获取token
  let token = store.getters['user/getUserInfo'].token
  if (token) {
    if (to.path === '/login' || to.path === '/') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    //判断是否在白名单中
    if (whiteList.indexOf(to.path) !== -1) {
      //存在白名单，放行
      next()
    } else {
      //不存在，登录
      next({ path: '/login' })
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})

export default router
